<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../res/layui/css/layui.css">
    <style>
        #userInfoDialog {
            padding: 20px;
            width: 400px;
            display: none; /*隐藏层*/
        }

        #userInfoDialog p {
            padding: 10px 0;
        }
    </style>


</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header">管理员列表</div>
        <div class="layui-card-body">
            <table id="adminlist" lay-filter="adminFilter"></table>
        </div>
    </div>
</div>
<!--用户信息对话框-->
<div id="userInfoDialog">
    <form class="layui-form" lay-filter="adminInfoFilter">
        <!--隐藏域，用于向后端传递id-->
        <input type="hidden" name="id">
        <div class="layui-form-item">
            <label for="username" class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" id="username" name="username" readonly>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="password" class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input type="password" class="layui-input" id="password" name="password">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="email" class="layui-form-label">邮箱</label>
            <div class="layui-input-block">
                <input type="email" class="layui-input" id="email" name="email">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <input type="radio" name="status" value="0" title="禁用" checked>
                <input type="radio" name="status" value="1" title="启用">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" class="layui-btn layui-btn-normal" lay-submit lay-filter="saveAdminInfo">修改</button>
                <button type="button" class="layui-btn layui-btn-primary" lay-on="cancel">取消</button>
            </div>
        </div>
    </form>
</div>

<script src="../res/layui/layui.js"></script>
<script>
    layui.use(function () {
        //加载获取table模块
        var table = layui.table;
        //加载获取layer弹层模块
        var layer = layui.layer;
        //加载jquery对象
        var $ = layui.jquery;
        var util = layui.util;
        var form = layui.form;

        util.on('lay-on',{
            'cancel':function(){
                //关闭弹层
                layer.closeAll()
            }
        })

        //数据表格渲染
        table.render({
            elem:'#adminlist'   //需要展示数据的表格元素选择器（ID选择器）
            ,url:'http://127.0.0.1:8080/admin/list'  //数据访问接口地址
            ,page:{
                limit:5    //每页显示数据条数
                ,limits:[3,5,10,15,20]  //候选每页数据行数列表
            }
            ,cols:[[
                {title:'序号',type:'numbers'}
                ,{title:'账号',field:'username'}
                ,{title:'邮箱',templet:'<div>{{ d.email ? d.email : "未设置" }}</div>'}
                ,{title:'账号状态',templet:function(d){
                        if(d.status === 0){
                            return '<span class="layui-badge layui-bg-red">禁用</span>'
                        }else{
                            return '<span class="layui-badge layui-bg-blue">正常</span>'
                        }
                    }
                 }
                ,{title:'操作',width:280,templet:'#rowTools'}
            ]]
        })


        //绑定单元格工具事件
        table.on('tool(adminFilter)',function(obj){
            //获取当前操作数据行
            let data = obj.data
            //获取当前被点击的按钮lay-event属性
            let layEvent = obj.event
            //根据事件名称执行对应的逻辑
            switch(layEvent){
                case 'edit':
                    //显示弹出框
                    layer.open({
                        type:1,
                        title:'编辑管理员信息',
                        content:$('#userInfoDialog'),
                        success:function(){
                            //填充数据
                            form.val('adminInfoFilter',data)
                        }
                    })
                    break
                case 'del':
                    layer.confirm('确认删除该管理员吗?',{icon:3},function(){
                        $.ajax({
                            type:'DELETE',         //设置请求方式
                            url:'http://127.0.0.1:8080/admin/'+data.id,     //服务端处理请求地址
                            success:function(resp){ //成功之后的回调
                                layer.msg(resp.msg)
                                //刷新数据表格
                                table.reload('adminlist')
                            }
                        })
                    })
                    break
                case 'updateStatus':
                    //获取需要修改的状态值  data-status
                    let status = $(this).data('status')
                    layer.confirm('确认修改账号状态吗?',{icon:3},function(){
                        //发送修改请求
                        $.ajax({
                            type:'PUT',         //设置请求方式
                            url:'http://127.0.0.1:8080/admin',     //服务端处理请求地址
                            contentType:'application/json',     //发送数据格式
                            data:JSON.stringify({       //发送的数据行
                                id:data.id,
                                status:status
                            }),
                            success:function(resp){ //成功之后的回调
                                layer.msg(resp.msg)
                                //刷新数据表格
                                table.reload('adminlist')
                            }
                        })
                    })
                    break
            }
        })

        //为弹层表单绑定提交事件
        form.on('submit(saveAdminInfo)',function(obj){
            //关闭所有弹层
            layer.closeAll()
            //获取表单字段
            let data = obj.field
            //显示加载层
            let layerIndex = layer.load(0)
            //发送修改请求
            $.ajax({
                type:'PUT',         //设置请求方式
                url:'http://127.0.0.1:8080/admin',     //服务端处理请求地址
                contentType:'application/json',     //发送数据格式
                data:JSON.stringify(data),       //发送的数据行
                success:function(resp){ //成功之后的回调
                    //关闭加载动画
                    layer.close(layerIndex)
                    layer.msg(resp.msg)
                    //刷新数据表格
                    table.reload('adminlist')
                }
            })
        })
    })
</script>


<!--操作栏自定义模板-->
<script id="rowTools" type="text/html">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">
            <i class="layui-icon layui-icon-edit"></i>
            编辑
        </button>
        <button class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">
            <i class="layui-icon layui-icon-delete"></i>
            删除
        </button>
        {{# if(d.status === 1){ }}
        <button class="layui-btn layui-btn-warm layui-btn-xs" lay-event="updateStatus" data-status="0">
            <i class="layui-icon layui-icon-close-fill"></i>
            禁用
        </button>
        {{# }else{ }}
        <button class="layui-btn layui-btn-normal layui-btn-xs" lay-event="updateStatus" data-status="1">
            <i class="layui-icon layui-icon-ok-circle"></i>
            启用
        </button>
        {{# } }}
    </div>
</script>


</body>
</html>